package org.zht.dormpro.dorm.controller.base;

import lombok.extern.log4j.Log4j;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.pub.ResponseMap;

import java.util.HashMap;

//执行controller中方法的回调
@Log4j
public class AbsBaseController {
    /**
     * 普通操作逻辑判断
     * @param actionDesc 操作描述
     * @param callback 操作的controller
     * @return 返回操作结果
     */
    public ResponseMap handle(BaseControllerCallback callback, String actionDesc,boolean ok) {
        String errorDesc =actionDesc,successDesc = actionDesc ;
        if(ok){
            successDesc = actionDesc + "成功!";
            errorDesc = actionDesc + "失败!";
        }
        boolean handle = false;
        try {
            handle = callback.handle();
            if (handle) {
                return ResponseMap.success(successDesc);
            } else {
                return ResponseMap.error(errorDesc);
            }
        } catch (Exception e) {
            log.error(e,e);
            return ResponseMap.error(errorDesc);
        }
    }
    public HashMap<String, Object> setTotal(PageBean pageBean){
        HashMap<String,Object> attr = new HashMap<>();
        attr.put("total",pageBean.getTotal());
        return attr;
    }
}
